package com.hm.manage.service.impl;

import java.util.List;
import com.hm.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.hm.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hm.manage.domain.Xtxjsub;
import com.hm.manage.mapper.XtxjMapper;
import com.hm.manage.domain.Xtxj;
import com.hm.manage.service.IXtxjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 系统巡检Service业务层处理
 * 
 * @author hm
 * @date 2025-04-16
 */
@Service
public class XtxjServiceImpl extends ServiceImpl<XtxjMapper,Xtxj> implements IXtxjService
{
    @Autowired
    private XtxjMapper xtxjMapper;

    /**
     * 查询系统巡检
     * 
     * @param id 系统巡检主键
     * @return 系统巡检
     */
    @Override
    public Xtxj selectXtxjById(Long id)
    {
        return xtxjMapper.selectXtxjById(id);
    }

    /**
     * 查询系统巡检列表
     * 
     * @param xtxj 系统巡检
     * @return 系统巡检
     */
    @Override
    public List<Xtxj> selectXtxjList(Xtxj xtxj)
    {
        return xtxjMapper.selectXtxjList(xtxj);
    }

    /**
     * 新增系统巡检
     * 
     * @param xtxj 系统巡检
     * @return 结果
     */
    @Transactional
    @Override
    public int insertXtxj(Xtxj xtxj)
    {
        xtxj.setCreateTime(DateUtils.getNowDate());
        int rows = xtxjMapper.insertXtxj(xtxj);
        insertXtxjsub(xtxj);
        return rows;
    }

    /**
     * 修改系统巡检
     * 
     * @param xtxj 系统巡检
     * @return 结果
     */
    @Transactional
    @Override
    public int updateXtxj(Xtxj xtxj)
    {
        xtxj.setUpdateTime(DateUtils.getNowDate());
        xtxjMapper.deleteXtxjsubByPid(xtxj.getId());
        insertXtxjsub(xtxj);
        return xtxjMapper.updateXtxj(xtxj);
    }

    /**
     * 批量删除系统巡检
     * 
     * @param ids 需要删除的系统巡检主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteXtxjByIds(Long[] ids)
    {
        xtxjMapper.deleteXtxjsubByPids(ids);
        return xtxjMapper.deleteXtxjByIds(ids);
    }

    /**
     * 删除系统巡检信息
     * 
     * @param id 系统巡检主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteXtxjById(Long id)
    {
        xtxjMapper.deleteXtxjsubByPid(id);
        return xtxjMapper.deleteXtxjById(id);
    }

    /**
     * 新增系统巡检子信息
     * 
     * @param xtxj 系统巡检对象
     */
    public void insertXtxjsub(Xtxj xtxj)
    {
        List<Xtxjsub> xtxjsubList = xtxj.getXtxjsubList();
        Long id = xtxj.getId();
        if (StringUtils.isNotNull(xtxjsubList))
        {
            List<Xtxjsub> list = new ArrayList<Xtxjsub>();
            for (Xtxjsub xtxjsub : xtxjsubList)
            {
                xtxjsub.setPid(id);
                list.add(xtxjsub);
            }
            if (list.size() > 0)
            {
                xtxjMapper.batchXtxjsub(list);
            }
        }
    }
}
